package com.qfhd.soft.cjzcore.Moudle;


import com.qfhd.soft.cjzcore.CJZConstant;
import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.Core.CJTime;
import com.qfhd.soft.cjzcore.Model.CardInfo;
import com.qfhd.soft.cjzcore.UI.Core.DecimalFormatHelper;
import com.qfhd.soft.cjzcore.User.CJZUser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CJGGKHelper {
    public static final int CARD_TYPE_COIN = 0;
    public static final int CARD_TYPE_CASH = 1;
    public static final int CARD_TYPE_CANCASH = 2;

    public static int ScratchAdTime = 3;
    private List<CardInfo> ggkList = new ArrayList<CardInfo>();
    private String card_sn = null;
    private int cardType = -1;
    private double amount = 0f;

    private static CJGGKHelper cjggkHelper;

    public static CJGGKHelper getInstance() {
        if (cjggkHelper == null) {
            synchronized (CJGGKHelper.class) {
                cjggkHelper = new CJGGKHelper();
            }
        }
        return cjggkHelper;
    }

    public void setGGkList(JSONArray jsonArray) {
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                JSONObject obj = jsonArray.getJSONObject(i);
                CardInfo tmp = CardInfo.getModelByJSONObject(obj);
                ggkList.add(tmp);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        CJLogger.v("ggkList size:" + ggkList.size());
    }

    public int getSize() {
        return ggkList.size();
    }

    public void putWatchCard(String sn) {
        CJLogger.v("putWatchCard sn:" + sn);
        card_sn = sn;
    }

    public void removeWatchCard() {
        CJLogger.v("removeWatchCard");
        card_sn = null;
        cardType = -1;
        amount = 0.0f;
    }

    public void getCardDetail(JSONObject obj) {
        try {
            if (obj.has("card_type")) {
                cardType = obj.getInt("card_type");
            }
            if (obj.has("amount")) {
                amount = obj.getDouble("amount");
            }
        } catch (JSONException e) {
            CJLogger.v("getCardDetail error:" + e.toString());
        }
    }

    public String getCardAward() {
        String str = "";
        switch (cardType) {
            case CARD_TYPE_COIN: {
                str = "金币 +" + DecimalFormatHelper.getInt((float) amount);
            }
            break;
            case CARD_TYPE_CASH: {
                str = "现金 +" + DecimalFormatHelper.getFloat2((float) (amount / 100)) + "元";
            }
            break;
            case CARD_TYPE_CANCASH: {
                str = "提现卡 +" + DecimalFormatHelper.getFloat2((float) (amount / 100)) + "元";
            }
            break;
            default:
                break;
        }
        return str;
    }

    public String getCardAward_str_number() {
        float str = 0.0f;
        switch (cardType) {
            case CARD_TYPE_COIN: {
                str = (float) amount;
                return "" + DecimalFormatHelper.getInt(str) + "金币";
            }
            case CARD_TYPE_CASH: {
                str = (float) (amount / 100);
                return "" + str + "元";
            }
            case CARD_TYPE_CANCASH: {
                str = (float) (amount / 100);
                return "" + DecimalFormatHelper.getInt(str) + "元提现卡";
            }
            default:
                break;
        }
        return "" + str;
    }

    public int getCardType() {
        return cardType;
    }

    public void addUserCoinOrCash() {
        switch (cardType) {
            case CARD_TYPE_COIN: {
                CJZUser.getInstance().addCoin((int) amount);
            }
            break;
            case CARD_TYPE_CASH: {
                CJZUser.getInstance().addCash((float) amount);
            }
            break;
            case CARD_TYPE_CANCASH: {
                CJLogger.v("can cash");
            }
            break;
            default:
                break;
        }
    }

    public String getCard_sn() {
        return card_sn;
    }

    public void changeStatusToUsed(String card_sn) {
        CJLogger.v("changeStatusToUsed sn:" + card_sn);
        for (CardInfo cardInfo : ggkList) {
            if (cardInfo.cards_sn.equalsIgnoreCase(card_sn)) {
                cardInfo.status = CardInfo.status_used;
                break;
            }
        }
    }

    public int getUnUsedCardSize() {
        int count = 0;
        for (CardInfo cardInfo : ggkList) {
            if (cardInfo.status == CardInfo.status_notuse) {
                count++;
            }
        }
        return count;
    }

    public String getOneNotUsedSN() {
        for (CardInfo cardInfo : ggkList) {
            if(cardInfo.status == CardInfo.status_notuse) {
                CJLogger.v("getOneNotUsedSN:" + cardInfo.cards_sn);
                return cardInfo.cards_sn;
            }
        }
        return null;
    }

    public String getNextTime() {
        return CJTime.getTimeFormatHHMM(getNextTime(CJTime.getTimestamp()));
    }

    private long getNextTime(long nowTime) {
        long time_24 = CJTime.getDayStartTimestamp(CJTime.getTimestamp()) + (24 * 60 * 60);
        for (int i = 0; i < CJZConstant.GGK.CardRefrshTime.size(); i++) {
//            CJLogger.v("refresh time:" + CJZConstant.GGK.CardRefrshTime.get(i));
            long tmp = CJTime.getDayStartTimestamp(CJTime.getTimestamp()) + (CJZConstant.GGK.CardRefrshTime.get(i) * 60 * 60);
            if (nowTime <= tmp) {
                return tmp;
            }
        }
        return time_24;
    }
}
